layui.use(['form', 'table', 'eleTree', 'fsoft'], function () {
    var $ = layui.$
        , form = layui.form
        , table = layui.table
        , eleTree = layui.eleTree
        , fsoft = layui.fsoft;
    var tableId = "sysmenu-tbs";
    var searchFormId = "fsoftSearchForm";
    var treeId = "fsoft-menuTrees";
    var menuTrees = eleTree.render({
        elem: '#' + treeId,
        url: layui.cache['contentPath'] + '/sys-menu/findMenuTrees',
        where: { menuTypeNotIn: 2 },
        request: { name: "title", key: "id", children: "children" },
        highlightCurrent: true,
        defaultExpandAll: true,
        expandOnClickNode: false,
        showLine: true
        , done: function (res) {
            active.doSearch();
        }
    });
    // 节点点击事件
    eleTree.on("nodeClick(" + treeId + ")", function (d) {
        var currentData = d.data.currentData;
        $("#parents").val(currentData.id);
        active.doSearch();
    });
    // 搜索监听
    form.on('submit(sreach)', function (data) {
        active.doSearch(data.field);
        return false;
    });
    // 工具栏监听 （批量操作）
    var active = {
        doSearch: function (params) {
            if (params == undefined || params == null)
                params = $.extend({ orderSortStr: "ORDER BY T.PARENTS" }, form.val(searchFormId));
            else
                $.extend(params, { orderSortStr: "ORDER BY T.PARENTS" });
            table.render({
                elem: '#' + tableId,
                toolbar: '#batchBtnToolBar',
                where: params,
                url: layui.cache['contentPath'] + '/sys-menu/findList',
                cols: [[
                    { type: 'checkbox', fixed: 'left' },
                    { type: 'numbers', fixed: 'left', title: '序号', align: 'center' },
                    { field: 'name', title: '名称', width: 150, sort: true },
                    { field: 'parentName', title: '上级', width: 150 },
                    {
                        field: 'menuType', title: '类型', width: 80, align: 'center', templet: function (item) {
                            if (item.menuType == 0) {
                                return '<span class="layui-badge layui-bg-cyan">目录</span>'
                            } else if (item.menuType == 1) {
                                return '<span class="layui-badge layui-bg-blue">菜单</span>'
                            } else if (item.menuType == 2) {
                                return '<span class="layui-badge">按钮</span>'
                            } else {
                                return '<span class="layui-badge layui-bg-primary">其他</span>'
                            }
                        }
                    },
                    {
                        field: 'icon', title: '图标', width: 60, align: 'center'
                        , templet: function (item) {
                            if (item.icon != undefined && item.icon != null && item.icon != "") {
                                return '<i class="' + item.icon + '" aria-hidden="true" ></i>';
                            }
                            return "";
                        }
                    },
                    { field: 'perms', title: '权限标识', width: 200 },
                    { field: 'actionUrl', title: '功能URL', width: 200 },
                    { fixed: 'right', width: 150, align: 'center', title: '操作', toolbar: '#tbRowBar', fixed: 'right' }
                ]],
                page: { limit: 20, limits: [20, 30, 50, 80, 100], theme: '#1E9FFF' },
                height: 'full-108'
            });
        }
        , removeBatch: function (data) { //批量删除
            if (data == undefined || data.length == 0) {
                layer.msg("请至少选择一条记录", { icon: 7, time: 3000 });
                return;
            }
            var ids = new Array();
            $.each(data, function (idx, item) {
                ids.push(item.id);
            });
            var msg = "确认要删除当前记录？";
            if (ids.length > 1)
                msg = "确认要删除已选中的（" + ids.length + "）条记录？";
            fsoft.confirm("询 问", msg, function (index) {
                fsoft.delete(layui.cache['contentPath'] + '/sys-menu/remove', ids, function (rs) {
                    if (rs.code == 0) {
                        fsoft.successMsg("删除成功");
                        active.doSearch();
                    } else {
                        fsoft.errorMsg("删除失败:" + rs.msg);
                    }
                });
                layer.close(index);
            });
        },
        // 添加菜单/目录/按钮
        showContentForm: function (data) {
            var path = layui.cache['contentPath'] + '/sys-menu/add';
            var title = "添加菜单";
            if (data) {
                path = layui.cache['contentPath'] + '/sys-menu/edit/' + data.id;
                title = data.name + "-编辑菜单";
            }
            layer.open({
                type: 2, title: title
                , area: ['500px', '525px']
                , content: path
                , end: function () {
                    menuTrees.reload();
                    active.doSearch();
                }
            });
        }
    };
    /* 工具条 */
    table.on('toolbar(' + tableId + ')', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case "addMenu":
                active.showContentForm();
                break;
            case "removeBatch":
                active.removeBatch(checkStatus.data);
                break;
        }
    });
    //行操作监听 
    table.on('tool(' + tableId + ')', function (obj) {
        var data = obj.data;
        if (obj.event === 'detail') {
            xadmin.open(data.userName + '-详情', layui.cache['contentPath'] + '/sys-menu/info/' + data.id, 500, 510);
        } else if (obj.event === 'edit') {
            active.showContentForm(data);
        } else if (obj.event === 'del') {
            active.removeBatch([data]);
        }
    });
});